package com.epfresh.api.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.supplier.R;
import com.epfresh.api.utils.AppUtils;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.utils.T;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

public class ShareDialog extends Dialog implements View.OnClickListener {

    private View vCancel;
    private View vQQ;
    private View vQQZone;
    private View vWechat;
    private View vFriendly;
    private View vCopyUrl;
    private Activity activity;

    public ShareDialog(Activity context) {

        this(context, R.style.ActionSheetDialogStyle);

    }

    public ShareDialog(Activity context, int theme) {
        super(context, theme);
        activity = context;
        builer();
    }

    public ShareDialog builer() {

        View v = LayoutInflater.from(getContext()).inflate(R.layout.layout_share, null);

        setContentView(v);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS;
        dialogWindow.setAttributes(lp);

        vCancel = findViewById(R.id.v_cancel);
        vQQ = findViewById(R.id.rl_qq);
        vQQZone = findViewById(R.id.rl_qqzone);
        vWechat = findViewById(R.id.rl_wechat);
        vFriendly = findViewById(R.id.rl_wechat_friendly);
        vCopyUrl = findViewById(R.id.tv_copy_share_url);
        vCancel.setOnClickListener(this);
        vQQ.setOnClickListener(this);
        vQQZone.setOnClickListener(this);
        vWechat.setOnClickListener(this);
        vFriendly.setOnClickListener(this);
        vCopyUrl.setOnClickListener(this);
        return this;
    }

    @Override
    public void show() {


    }

    public void showDialog() {
        super.show();
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (v.getId() == R.id.tv_copy_share_url) {
            if (pageUrl == null) {
                pageUrl = "";
            }
            shareUrl(pageUrl);
        } else {
            share(v.getId());
        }
//        switch (v.getId()) {
//            case R.id.v_cancel:
//
//                break;
//            case R.id.rl_qq:
//
//                break;
//            case R.id.rl_qqzone:
//
//                break;
//            case R.id.rl_wechat:
//
//                break;
//            case R.id.rl_wechat_friendly:
//
//                break;
//            case R.id.tv_copy_share_url:
//                shareUrl("e批生鲜");
//                break;
//            default:
//                break;
//        }
    }


    private String pageUrl;
    private String imageUrl;
    private String text;
    private String title;
    private String id;

    public void setShareContent(String url, String imageUrl, String text, String title, String id) {
        this.pageUrl = url;
        this.imageUrl = imageUrl;
        this.text = text;
        this.title = title;
        this.id = id;
    }

    public void showDialog(String url, String imageUrl, String text, String title, String id) {
        this.setShareContent(url, imageUrl, text, title, id);
        super.show();
    }

    public void showDialog(String url, String imageUrl, String text, String title) {
        this.setShareContent(url, imageUrl, text, title, null);
        super.show();
    }

    //供货端 //1105418748 0JAlakouvQUNS5Iy
    private void share(int type) {
        UMImage image = null;
        if (imageUrl != null && !"".equals(imageUrl)) {
            image = new UMImage(activity, imageUrl);
        }
        String url = this.pageUrl;
        String text = this.text;
        String title = this.title;
        final UMWeb web = new UMWeb(url, title, text, image); //切记切记 这里分享的链接必须是http开头
//        web.setTitle(title);//标题
//        web.setThumb(image);  //缩略图
//        web.setDescription(text);//描述
        switch (type) {
            case R.id.rl_qq:
                new ShareAction(activity).setPlatform(SHARE_MEDIA.QQ)
                        .withMedia(web)
                        .setCallback(umShareListener)
                        .share();
                break;
            case R.id.rl_qqzone:
                new ShareAction(activity).setPlatform(SHARE_MEDIA.QZONE)
                        .withMedia(web)
                        .setCallback(umShareListener)
                        .share();
                break;
            case R.id.rl_wechat:
                new ShareAction(activity).setPlatform(SHARE_MEDIA.WEIXIN)
                        .withMedia(web)
                        .setCallback(umShareListener)
                        .share();

                break;
            case R.id.rl_wechat_friendly:
                new ShareAction(activity).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                        .withMedia(web)
                        .setCallback(umShareListener)
                        .share();
                break;
            default:
                break;
        }

    }

    private void shareUrl(String content) {
        AppUtils.copy(content, getContext());
        if (activity != null) {
            T.toast(BaseApplication.getDoc().getCopyed());
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
//            if (activity != null)
//                T.toast(BaseApplication.getDoc().getShared());
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (activity != null)
                T.toast("分享失败:" + t.getMessage());
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            if (activity != null)
                T.toast("分享被取消了");
        }
    };


    public void onResult(int requestCode, int resultCode, Intent data) {
        if (activity != null)
            UMShareAPI.get(activity).onActivityResult(requestCode, resultCode, data);
    }


}
